package com.lqq.controller.restfull;

import com.alibaba.fastjson.JSON;
import com.lqq.api.AccountService;
import com.lqq.api.ProductService;
import com.lqq.dto.AccountDto;
import com.lqq.dto.OrderDto;
import com.lqq.dto.ProductDto;
import com.lqq.entity.OrderEntity;
import com.lqq.repository.OrderRepository;
import com.lqq.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * @author qingqing.liang
 * @create 2018-01-30
 **/
@RestController
@Api(description = "业务测试接口，调用订单系统和仓库系统")
public class UserController {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private ProductService productService;

    @Resource
    private AccountService accountService;


    @RequestMapping("/info")
    @ApiOperation(value = "查看当前订单系统和仓库系统信息",httpMethod = "GET")
    public String info(@RequestParam @ApiParam(required = true,defaultValue = "1") Long id){
        List<OrderEntity> orders  = orderRepository.findAll();
        AccountDto account = accountService.getById(1L);
        ProductDto product = productService.getById(1L);

        return "订单信息：" +JSON.toJSONString(orders) + "\r"
                +"库存信息：" +JSON.toJSONString(product) + "\r"
                +"账户信息：" +JSON.toJSONString(account) + "\r";
    }

    @RequestMapping("/orderSuccess")
    @ApiOperation(value = "下单成功操作",httpMethod = "GET")
    public String orderSuccess(){
        //
        OrderDto orderDto = new OrderDto();
        orderDto.setName("支付成功的订单");
        orderDto.setMoney(BigDecimal.valueOf(5L));
        orderService.order(orderDto);
        return "下单成功";
    }




    @RequestMapping("/orderFail")
    @ApiOperation(value = "下单失败操作",httpMethod = "GET")
    public String orderFail(){
        //
        OrderDto orderDto = new OrderDto();
        orderDto.setName("支付失败的订单");
        //将订单金额故意设置为负数，让账户系统报错，查看回滚
        orderDto.setMoney(BigDecimal.valueOf(-1L));
        orderService.order(orderDto);
        return "下单失败";
    }




}
